package com.glodon.paas.account.service.impl;

import java.util.ArrayList;
import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.glodon.paas.account.api.bean.Communicate;
import com.glodon.paas.account.biz.impl.CommunicateBookBizImpl; 
import com.glodon.paas.account.dao.domain.CommunicateEntity; 
import com.glodon.paas.account.service.CommunicateBookService;

@Component("communicateBookService")
public class CommunicateBookServiceImpl implements CommunicateBookService {
	@Autowired
	private CommunicateBookBizImpl communicateBookBiz;
 
	public String addCommunicate(
			String userId,			// 用户ID
			String contactId,		// 联系人ID
			String remarkName,		// 用户备注的联系人姓名,默认使用Email
			boolean updateIfExis
			){
		CommunicateEntity communicateEntity = new CommunicateEntity();	
		communicateEntity.setUserId(userId);
		communicateEntity.setContactId(contactId);
		communicateEntity.setRemarkName(remarkName);		
		return communicateBookBiz.add(communicateEntity,updateIfExis);
	}

	
	public Integer removeById(
			String id
			){
		return communicateBookBiz.removeById(id);		
	}
	
	
	public Integer remove(
			String userId,			// 用户ID
			String contactId		// 联系人ID 
			){		 
		int id = communicateBookBiz.remove(userId, contactId);		
		return id;
	}
	
	public Integer updateOrder(
			String id, 				// id
			Integer newOrder, 		// 新序号
		Integer orderType		// 排列顺序，1递增，-1递减
			){
		return communicateBookBiz.updateOrder(id, newOrder, orderType);
	}
	
	
	public Integer updateRemark(
			String id,
			String remarkName,
			String remarkText
			){ 
		return communicateBookBiz.updateRemark(id, remarkName, remarkText);
	}
	
	
	public String count(
			String userId
			){
		return String.valueOf(communicateBookBiz.count(userId));
	}
	
	public Communicate queryById(
			String id
			){
		return converteToDto(communicateBookBiz.queryById(id), true);
	}
	
	
	public Communicate queryByContactId(
			String userId,
			String contactId
			){
		return converteToDto(communicateBookBiz.queryByContactId(userId, contactId), true);
	}
	
	
	public List<Communicate> query(
			String userId,
			Integer orderType,
			Integer startIndex,
			Integer count
			){
		return converteToDto(communicateBookBiz.query(userId, orderType, startIndex, count));
	}
	
	public Integer judge(
			String userId,
			String contactId
			){
		return communicateBookBiz.judge(userId, contactId);
	}
		
	private List<Communicate> converteToDto(List<CommunicateEntity> domainList) { 
		List<Communicate> list = new ArrayList<Communicate>();
		for(CommunicateEntity item : domainList){
			list.add(converteToDto(item, false));
		}
		// 批量查询填充relationEmail信息
		// ...
		return list;
	}
	
	private Communicate converteToDto(CommunicateEntity domain, boolean verifyEmail) { 
		if(domain == null){
			return null;
		}
		Communicate dto = new Communicate();
		dto.setCreateTime(domain.getCreateTime());
		dto.setId(domain.getId());
		dto.setOrderNum(domain.getOrderNum());		
		dto.setContactEmail(domain.getContactEmail());
		dto.setContactId(domain.getContactId());
		dto.setRemarkName(domain.getRemarkName());
		dto.setRemarkText(domain.getRemarkText());
		dto.setUserId(domain.getUserId());
		dto.setUpdateTime(domain.getUpdateTime());
		//TODO Email取自account
		if(verifyEmail){
			
		}
		return dto;
	}
}
